package com.ruoyi.logCen.kafka;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/testCon/kafka")
public class KafkaController {

    @Autowired
    private KafkaProducer kafkaProducer;

    @GetMapping("/send")
    public String send(@RequestParam("message") String message) {
        kafkaProducer.sendMessage("my-topic1", message);
        return "Message sent to the Kafka Topic";
    }

    @GetMapping("/send2")
    public String send2(@RequestParam("message") String message, @RequestParam("topic") String topic) {
        System.out.println("topic:"+topic);
        System.out.println("message:"+message);

        kafkaProducer.sendMessage(topic, message);
        return "Message sent to the Kafka Topic";
    }

    @PostMapping("/send3")
    public String send3(@RequestBody Map<String, Object> param) {

        String topic = param.get("topic").toString();
        Map<String, Object> messageMap = (Map<String, Object>) param.get("message");
        String message = JSON.toJSONString(messageMap);

        System.out.println("topic:"+topic);
        System.out.println("message:"+message);

        kafkaProducer.sendMessage(topic, message);
        return "Message sent to the Kafka Topic";
    }
}

